// 扩展API加载完毕后调用onPlusReady回调函数 
document.addEventListener("plusready", onPlusReady, false);
// 扩展API加载完毕，现在可以正常调用扩展API
function onPlusReady() {
	getLocation();
}

function getLocation() {
	if(navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(showPosition, showError);
	} else {
		mui.alert("不支持地理定位。","定位失败");
	}
}

function showError(error) {
	switch(error.code) {
		case error.PERMISSION_DENIED:
			mui.alert("定位失败,用户拒绝请求地理定位","定位失败");
			break;
		case error.POSITION_UNAVAILABLE:
			mui.alert("定位失败,位置信息不可用","定位失败");
			break;
		case error.TIMEOUT:
			mui.alert("定位失败,请求获取用户位置超时","定位失败");
			break;
		case error.UNKNOWN_ERROR:
			mui.alert("定位失败,定位系统失效","定位失败");
			break;
	}
}

function showPosition(position) {
	var latlon = position.coords.latitude + ',' + position.coords.longitude;
	var url = "http://api.map.baidu.com/geocoder/v2/?ak=W4umk36pvAasI43XAAhEE6iiYgxig1KK&callback=renderReverse&location=" + latlon + "&output=json&pois=0";
	$.ajax({
		type: "GET",
		dataType: "jsonp",
		url: url,
							//beforeSend: function() {
							//	$("#baidu_geo").html('正在定位...');
							//},
		success: function(json) {
			if(json.status == 0) {
				//							//console.log(JSON.stringify(json));
				//							alert(JSON.stringify(json));
				$(".location").text(json.result.addressComponent.city);
				if(json.result.addressComponent.city == "重庆市"){
					lset("location_href","重庆市");
					lset("are_flag","c");
					var index_id = plus.webview.getWebviewById("sub_find_index.html");
					var id_head = "500000";
					mui.fire(index_id, 'selectid', {
						id_head: id_head
					});
					lset("location_two_id","500000");
				}else{
					lset("location_href","三亚市");
					lset("are_flag","s");
					var index_id = plus.webview.getWebviewById("sub_find_index.html");
					var id_head_two = "460000";
					mui.fire(index_id, 'selectid', {
						id_head: id_head_two
					});
					lset("location_two_id","460000");
				}
			} else {
				alert("网络连接失败");
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			mui.toast("网络连接失败");
		}
	});
	//				需要转换经纬度
	//				var map = new BMap.Map("allmap");
	//				var point = new BMap.Point(116.331398, 39.897445);
	//				//	map.centerAndZoom(point,12);//初始化地图
	//				var geoc = new BMap.Geocoder();
	//
	//				geoc.getLocation(point, function(rs) {
	////					//console.log(JSON.stringify(rs));
	//					var addComp = rs.addressComponents;
	//					alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
	//				});

};